package com.admin.client.service;

import com.admin.client.mapper.CoreMapMapper;
import com.admin.client.domain.CoreMap;

import com.admin.client.util.ResponseUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;


/**
 * 战斗地图(CoreMap)表服务实现类
 *
 * @author cxy
 * @since 2025-01-09 10:28:28
 */
@Validated
@Slf4j
@Service
public class CoreMapServiceImpl extends ServiceImpl<CoreMapMapper, CoreMap>  {
    @Resource
    private CoreMapMapper coreMapMapper;
    /**
     * 导出 战斗地图(CoreMap)
     *
     * @param response HttpServletResponse
     * @throws IOException 异常
     */
    public void export(HttpServletResponse response) throws IOException {
        ResponseUtil.xlsxDownloadHandler(response, "战斗地图.xlsx");
        EasyExcel.write(response.getOutputStream(), CoreMap.class)
                .autoCloseStream(Boolean.FALSE)
                .sheet("core_map")
                .doWrite(list());
    }

    /**
     * 导入 战斗地图(CoreMap)
     *
     * @param multipartFile MultipartFile
     * @throws IOException 异常
     */
    public void importExcel(@NotNull MultipartFile multipartFile) throws IOException {
        coreMapMapper.clearTable();
        EasyExcel.read(multipartFile.getInputStream())
                .head(CoreMap.class)
                .doReadAllSync().forEach(po -> {
                    log.info("读取到一条[战斗地图]配置数据:{}", po);
                    save((CoreMap) po);
                });
    }
}
